package com.css.dzj.zfjg.modules.supervisionreportinfo.controller;


import com.css.aq.server.service.BaseService;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.common.util.ServiceUtil;
import com.css.dzj.zfjg.modules.supervisioncompinfo.entity.*;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgBlComplaintService;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgDfComplaintService;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgFbComplaintService;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgTsjbAccessoryService;
import com.css.dzj.zfjg.modules.supervisioncompinfo.vo.TsXqVo;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.JgFbJgjbEntity;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.JgTsjbReportInfoEntity;
import com.css.dzj.zfjg.modules.supervisionreportinfo.service.JgBlReportService;
import com.css.dzj.zfjg.modules.supervisionreportinfo.service.JgFbReportService;
import com.css.dzj.zfjg.modules.supervisionreportinfo.vo.ReportVo;
import com.css.dzj.zfjg.sys.user.entity.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/jgbldf/")
public class JgDfReportController {
    @Autowired
    BaseService baseService;
    @Autowired
    JgFbReportService jgFbReportService;
    @Autowired
    JgFbComplaintService jgFbComplaintService;
    @Autowired
    JgDfComplaintService jgDfComplaintService;
    @Autowired
    JgTsjbAccessoryService jgTsjbAccessoryService;
    @Autowired
    JgBlComplaintService jgBlComplaintService;
    @Autowired
    JgBlReportService jgBlReportService;
    @Autowired
    private ServiceUtil serviceUtil;
    /**
     * 获取答复信息页面
     * @param registerCode
     * @return 监管举报信息表  任务分办表  任务催办表
     */
    @GetMapping("getdfinfo")
    public R getdfinfo(@RequestParam String registerCode) {
        try{
            //监管举报信息
            JgTsjbReportInfoEntity jgTsjbReportInfoEntity = jgFbReportService.findReportInfo(registerCode);
            //举报人信息表
            JgTsjbUserInfoEntity jgTsjbUserInfoEntity = jgFbComplaintService.findUser(registerCode);
            //监管投诉任务分办表
            JgFbJgjbEntity jgFbJgjbEntity = jgFbReportService.findRegister(registerCode);
            //任务催办表
            JgTsjbUrgeInfoEntity jgTsjbUrgeInfoEntity = jgFbComplaintService.findCode(registerCode);

            ReportVo reportVo3 = new ReportVo();
            reportVo3.setJgTsjbReportInfoEntity(jgTsjbReportInfoEntity);
            reportVo3.setJgTsjbUserInfoEntity(jgTsjbUserInfoEntity);
            reportVo3.setJgFbJgjbEntity(jgFbJgjbEntity);
            reportVo3.setJgTsjbUrgeInfoEntity(jgTsjbUrgeInfoEntity);
            return R.ok().put("reportVo3", reportVo3);
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3, "通过任务编码获取任务详情失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 答复内容保存
     * @param reportVo
     */
    @PostMapping("save")
    public R save(@RequestBody ReportVo reportVo, HttpServletRequest request) {
        try{
            //保存答复信息
            JgTsjbHandleInfoEntity jgTsjbHandleInfoEntity = reportVo.getJgTsjbHandleInfoEntity();
            R r = serviceUtil.getUserInfoByRequest(request);
            LoginUser userInfo=new LoginUser();
            if(r.getCode().equals("200")) {
                userInfo =  (LoginUser)r.get("data");
            }
            jgTsjbHandleInfoEntity.setRegisterCode(reportVo.getJgTsjbReportInfoEntity().getRegisterCode());
            jgTsjbHandleInfoEntity.setHandleDepartmentName(userInfo.getOrgName());
            jgDfComplaintService.add(jgTsjbHandleInfoEntity);
            //保存调解信息
            JgTsjbMediateInfoEntity jgTsjbMediateInfoEntity = reportVo.getJgTsjbMediateInfoEntity();
            jgDfComplaintService.addmediate(jgTsjbMediateInfoEntity);
            //保存附件信息
          //  JgTsjbAccessoryEntity jgTsjbAccessoryEntity = reportVo.getJgTsjbAccessoryEntity();
            //jgTsjbAccessoryService.addfj(jgTsjbAccessoryEntity);
            //修改分办信息状态改为已答复104
            JgFbJgjbEntity jgFbJgjbEntity = reportVo.getJgFbJgjbEntity();
            String flag = "104";
            jgBlReportService.update(jgFbJgjbEntity,request,flag);
            //修改主表信息状态为已答复104
            JgTsjbReportInfoEntity jgTsjbReportInfoEntity =  reportVo.getJgTsjbReportInfoEntity();
            jgBlReportService.updatetsjb(jgTsjbReportInfoEntity,request,flag);
            return  R.ok();
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3, "提交失败", "2");
            return R.error("提交失败");
        }

    }


}
